#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@author: xiaodun
@date: 2025/9/1820:35
"""
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,NoSuchElementException
from dl_auto_test.config.config import config,BASE_DIR


class BasePage:
    def __init__(self,driver):
        self.driver=driver
        self.wait=WebDriverWait(self.driver,config.get_explicit_wait())
        self.base_url=config.get_env_url()

    def switch_to_frame(self,locator):
        iframe=self.find_element(locator)
        self.driver.switch_to.frame(iframe)

    def open(self,url):
        self.driver.get(url)

    def find_element(self,locator):
        try:
            return self.wait.until(EC.presence_of_element_located(locator))
        except TimeoutException:
            return []

    def click(self,locator):
        element=self.find_element(locator)
        element.click()

    def input_text(self,locator,text):
        element=self.find_element(locator)
        element.clear()
        element.send_keys(text)

    def get_text(self,locator):
        element=self.find_element(locator)
        return element.text

    def is_element_present(self,locator):
        try:
            self.find_element(locator)
            return True
        except NoSuchElementException:
            return False

    def wait_for_element_visible(self,locator):
        return self.wait.until(EC.visibility_of_element_located(locator))

    def wait_for_element_clickable(self,locator):
        return self.wait.until(EC.element_to_be_clickable(locator))

    def take_screenshot(self,filename):
        self.driver.save_screenshot(str(BASE_DIR/filename))

    # def search_locator_input(self,locator,text):
    #     element=self.find_element(locator)
    #     element.send_keys(text)

